********Clean timelines to get precise location, migration, and birth information********

***IFLS1

cd "$ifls1rawdata"

*Basic information
use bukkar2.dta, clear
keep pidlink hhid93 ar08mth ar08yr ar09yr ar07 ar13
rename ar07 sex
rename ar13 marstat
rename ar08mth birmth
rename ar08yr biryr
rename ar09yr age
la var age "age of respondent during interview IFLS1"

tempfile timelines
save "`timelines'", replace

*Merge location information
use bukksc1.dta, clear
keep hhid93 sc01 sc02 sc03 sc05
rename sc05 ivwtype
merge 1:m hhid93 using "`timelines'", gen(_merge1sc1)
drop if _merge1sc1==1
drop _merge1sc1

save "`timelines'", replace

*Merge with current date of interview
use bukkar1.dta, clear
keep hhid93 mth1_1
rename mth1_1 ivwmth
drop if ivwmth > 12
gen ivwyr = 0
replace ivwyr = 93 if ivwmth > 7
replace ivwyr = 94 if ivwmth < 7
la var ivwyr "IFLS1 interview year"
merge 1:m hhid93 using "`timelines'", gen(_merge1ar1)
drop if _merge1ar1 != 3
drop _merge1ar1

save "`timelines'", replace

*Merge with migration data, starting with birthplace and location at 12
use buk3mg1.dta, clear
keep pidlink hhid93 mg01a2 mg01b2 mg01c2 mg01d2 mg05a2 mg05b2 mg05c2 mg05d2 mg04 mg08

*Missing locations
drop if mg01b2==.
replace mg01b2 = 999 if mg01b2 > 750 | mg01b2 == 98 | mg01b2 == 96
drop if mg05b2==.
replace mg05b2 = 999 if mg05b2 > 750 | mg05b2 == 98 | mg05b2 == 96
drop if mg01c2==.
replace mg01c2 = 99 if mg01c2 > 78
drop if mg05c2==.
replace mg05c2 = 99 if mg05c2 > 78
drop if mg01d2==.
replace mg01d2 = 99 if mg01d2 > 82
drop if mg05d2==.
replace mg05d2 = 99 if mg05d2 > 82

*Creating location codes
gen kecbir = string(mg01b2, "%03.0f")
gen kabbir = string(mg01c2, "%02.0f")
gen probir = string(mg01d2, "%02.0f")
gen kec12 = string(mg05b2, "%03.0f")
gen kab12 = string(mg05c2, "%02.0f")
gen pro12 = string(mg05d2, "%02.0f")

gen locbir = probir + kabbir + kecbir
la var locbir "location at birth"
gen loc12 = pro12 + kab12 + kec12
la var loc12 "location when 12"

keep hhid93 pidlink mg04 mg08 locbir loc12
rename mg04 typebir
rename mg08 type12
merge 1:1 hhid93 pidlink using "`timelines'", gen(_mergebir)

gen kecivw = string(sc03, "%03.0f")
gen kabivw = string(sc02, "%02.0f")
gen proivw = string(sc01, "%02.0f")
gen locivw = proivw + kabivw + kecivw
la var locivw "Location HH lives when interviewed in IFLS1"
drop kecivw kabivw proivw sc01 sc02 sc03

save "`timelines'", replace


*Merge with migration data after 12
use buk3mg2.dta, clear

*Preserve number of moves
keep pidlink movenum
collapse (max) movenum, by(pidlink)

merge 1:1 pidlink using "`timelines'", gen(_mergemov1)
save "`timelines'", replace

*Record locations of moves
use buk3mg2.dta, clear
keep movenum mg21b2 mg21c2 mg21d2 mg24mth mg24yr mg25 mg27a mg28 mg34 mg35 mg36 pidlink

*Missing locations
replace mg21b2 = 999 if mg21b2 > 750 | mg21b2 == 98 | mg21b2 == 96 | mg21b2 == .
replace mg21c2 = 99 if mg21c2 > 78 | mg21c2 == .
replace mg21d2 = 99 if mg21d2 > 82 | mg21d2 == .

*Generate location of move variable
gen kecmov = string(mg21b2, "%03.0f")
gen kabmov = string(mg21c2, "%02.0f")
gen promov = string(mg21d2, "%02.0f")
gen locmov = promov + kabmov + kecmov
drop kecmov kabmov promov mg21b2 mg21c2 mg21d2

rename mg24mth mthmov
rename mg24yr yrmov
rename mg25 agemov
rename mg27 distmov
rename mg28 purpmov
rename mg34 othHHsmov
rename mg35 numHHsmov
rename mg36 whiHHsmov
rename movenum num
reshape wide *mov, i(pidlink) j(num)

merge 1:1 pidlink using  "`timelines'", gen(_mergemov2)

drop _mergebir _mergemov1 _mergemov2

foreach x of varlist _all {
	rename `x' I1_`x'
}

rename I1_pidlink pidlink


*Drop individuals who are in the sample but not interviewed
drop if I1_locbir==""
replace I1_movenum = 0 if I1_movenum==.
drop if I1_age == .

save "`timelines'", replace

***IFLS2

cd "$ifls2rawdata"

*Basic information
use b3a_cov, clear
keep hhid97 pidlink panel sex marstat age ivwmth1 ivwyr1 dob_mth dob_yr result reason
rename ivwmth1 ivwmth
rename ivwyr1 ivwyr
rename dob_mth birmth
rename dob_yr biryr

tempfile timelines2
save "`timelines2'", replace

*Merge location information
use bk_sc, clear
keep hhid97 sc01 sc02 sc03 mover97 sc05
rename sc05 ivwtype
gen kecivw = string(sc03, "%03.0f")
gen kabivw = string(sc02, "%02.0f")
gen proivw = string(sc01, "%02.0f")
gen locivw = proivw + kabivw + kecivw
la var locivw "Location HH lives when interviewed in IFLS2"
drop kecivw kabivw proivw sc01 sc02 sc03
merge 1:m hhid97 using "`timelines2'", gen(_mergecur)
drop if pidlink==""
save "`timelines2'", replace

*Merge with migration data, starting with birthplace and location at 12
use b3a_mg1, clear
keep pidlink mg00x mg01b mg01c mg01d mg04 mg05b mg05c mg05d mg08 mg04a

*Excluding panel respondents for which we have info from IFLS1
drop if mg00x == 1

*Creating location codes
replace mg01b = 999 if mg01b == .
replace mg01c = 99 if mg01c == .
replace mg01d = 99 if mg01d == .
gen kecbir = string(mg01b, "%03.0f")
gen kabbir = string(mg01c, "%02.0f")
gen probir = string(mg01d, "%02.0f")
gen locbir = probir + kabbir + kecbir
la var locbir "location at birth"
drop mg01b mg01c mg01d probir kabbir kecbir

replace mg05b = 999 if mg05b == . & mg04a != 1
replace mg05c = 99 if mg05c == . & mg04a != 1
replace mg05d = 99 if mg05d == . & mg04a != 1
gen kec12 = string(mg05b, "%03.0f")
gen kab12 = string(mg05c, "%02.0f")
gen pro12 = string(mg05d, "%02.0f")
gen loc12 = pro12 + kab12 + kec12
la var loc12 "location at 12yo"
replace loc12 = locbir if mg04a == 1
drop mg05b mg05c mg05d pro12 kab12 kec12 mg04a mg00x
rename mg04 typebir
rename mg08 type12
merge 1:1 pidlink using "`timelines2'", gen(_mergebir)
drop if _mergebir == 1
drop _mergebir _mergecur
save "`timelines2'", replace


*Merge with migration data after 12
use b3a_mg2, clear

*Preserve number of moves
keep pidlink movenum
collapse (max) movenum, by(pidlink)
merge 1:1 pidlink using "`timelines2'", gen (_mergenum)
replace movenum = 0 if movenum == .
drop _mergenum
save "`timelines2'", replace

*Record locations of moves
use b3a_mg2, clear
keep movenum pidlink mg21b mg21c mg21d mg24mth mg24yr mg25 mg27a mg28 mg34 mg35 mg36
replace mg21b = 999 if mg21b == .
replace mg21c = 99 if mg21c == .
replace mg21d = 99 if mg21d == .

gen kecmov = string(mg21b, "%03.0f")
gen kabmov = string(mg21c, "%02.0f")
gen promov = string(mg21d, "%02.0f")
gen locmov = promov + kabmov + kecmov
drop kecmov kabmov promov mg21b mg21c mg21d

rename mg24mth mthmov
rename mg24yr yrmov
rename mg25 agemov
rename mg27a distmov
rename mg28 purpmov
rename mg34 othHHsmov
rename mg35 numHHsmov
rename mg36 whiHHsmov
rename movenum num
reshape wide *mov, i(pidlink) j(num)

merge 1:1 pidlink using "`timelines2'", gen(_mergemov)
drop _merge _mergemov

foreach x of varlist _all {
	rename `x' I2_`x'
}

rename I2_pidlink pidlink
save "`timelines2'", replace


***IFLS3

cd "$ifls3rawdata"

*Basic information
use b3a_cov, clear
keep hhid00 pidlink panel sex marstat age ivwmth1 dob_mth dob_yr result reason
rename ivwmth1 ivwmth
gen ivwyr = 2000
la var ivwyr "date of visit (year)"
replace ivwyr = 2001 if ivwmth == 1
rename dob_mth birmth
rename dob_yr biryr
tempfile timelines3
save "`timelines3'", replace

*Merge location information
use bk_sc, clear
keep hhid00 sc01 sc02 sc03 sc05
rename sc05 ivwtype
gen kecivw = string(sc03, "%03.0f")
gen kabivw = string(sc02, "%02.0f")
gen proivw = string(sc01, "%02.0f")
gen locivw = proivw + kabivw + kecivw
la var locivw "Location HH lives when interviewed in IFLS3"
drop kecivw kabivw proivw sc01 sc02 sc03
merge 1:m hhid00 using "`timelines3'", gen(_mergecur)
drop if pidlink==""
save "`timelines3'", replace

*Merge with migration data, starting with birthplace and location at 12
use b3a_mg1, clear
keep pidlink mg01b mg01c mg01d mg04 mg05b mg05c mg05d mg08 mg04a

replace mg01b = 999 if mg01b > 740
replace mg01c = 99 if mg01c > 78
replace mg01d = 99 if mg01d > 82
gen kecbir = string(mg01b, "%03.0f")
gen kabbir = string(mg01c, "%02.0f")
gen probir = string(mg01d, "%02.0f")
gen locbir = probir + kabbir + kecbir
la var locbir "location at birth"
drop mg01b mg01c mg01d probir kabbir kecbir
replace mg05b = 999 if mg05b > 730 & mg04a != 1
replace mg05c = 99 if mg05c > 78 & mg04a != 1
replace mg05d = 99 if mg05d > 74 & mg04a != 1
gen kec12 = string(mg05b, "%03.0f")
gen kab12 = string(mg05c, "%02.0f")
gen pro12 = string(mg05d, "%02.0f")
gen loc12 = pro12 + kab12 + kec12
la var loc12 "location at 12yo"
replace loc12 = locbir if mg04a == 1
drop mg05b mg05c mg05d pro12 kab12 kec12 mg04a
rename mg04 typebir
rename mg08 type12
merge 1:1 pidlink using "`timelines3'", gen(_mergebir)
drop if _mergebir == 1
drop _mergebir _mergecur
save "`timelines3'", replace

*Merge with migration data after 12
use b3a_mg2, clear

*Preserve number of moves
keep pidlink movenum
collapse (max) movenum, by(pidlink)
merge 1:1 pidlink using "`timelines3'", gen (_mergenum)
replace movenum = 0 if movenum == .
drop _mergenum
save "`timelines3'", replace

*Record locations of moves
use b3a_mg2, clear
keep movenum pidlink mg21b mg21c mg21d mg24mth mg24yr mg25 mg27 mg28 mg34 mg35 mg36
replace mg21b = 999 if mg21b > 740
replace mg21c = 99 if mg21c > 78
replace mg21d = 99 if mg21d > 93

gen kecmov = string(mg21b, "%03.0f")
gen kabmov = string(mg21c, "%02.0f")
gen promov = string(mg21d, "%02.0f")
gen locmov = promov + kabmov + kecmov
drop kecmov kabmov promov mg21b mg21c mg21d

rename mg24mth mthmov
rename mg24yr yrmov
rename mg25 agemov
rename mg27 distmov
rename mg28 purpmov
rename mg34 othHHsmov
rename mg35 numHHsmov
rename mg36 whiHHsmov
rename movenum num
reshape wide *mov, i(pidlink) j(num)

merge 1:1 pidlink using "`timelines3'", gen(_mergemov)
drop _merge _mergemov

foreach x of varlist _all {
	rename `x' I3_`x'
}

rename I3_pidlink pidlink
drop if I3_locbir==""
save "`timelines3'", replace

***IFLS4

cd "$ifls4rawdata"

*Basic information
use b3a_cov, clear
keep hhid07 pidlink panel sex marstat age ivwmth1 ivwyr1 dob_mth dob_yr result reason
rename ivwmth1 ivwmth
rename ivwyr1 ivwyr
replace ivwyr = 2007 if ivwyr == 7
replace ivwyr = 2008 if ivwyr == 8
rename dob_mth birmth
rename dob_yr biryr

tempfile timelines4
save "`timelines4'", replace

**Add current location
use bk_sc, clear
keep hhid07 sc010707 sc020707 sc030707 sc05
rename sc05 ivwtype
gen kecivw = string(sc030707, "%03.0f")
gen kabivw = string(sc020707, "%02.0f")
gen proivw = string(sc010707, "%02.0f")
gen locivw = proivw + kabivw + kecivw
la var locivw "Location HH lives when interviewed in IFLS4"
drop kecivw kabivw proivw sc010707 sc020707 sc030707
merge 1:m hhid07 using "`timelines4'", gen(_mergecur)
drop if pidlink==""
save "`timelines4'", replace

*Merge with migration data, starting with birthplace and location at 12
use b3a_mg1, clear
keep pidlink mg01b mg01c mg01d mg04 mg05b mg05c mg05d mg08 mg04a

replace mg01b = 999 if mg01b > 740
replace mg01c = 99 if mg01c > 78
replace mg01d = 99 if mg01d > 82
gen kecbir = string(mg01b, "%03.0f")
gen kabbir = string(mg01c, "%02.0f")
gen probir = string(mg01d, "%02.0f")
gen locbir = probir + kabbir + kecbir
la var locbir "location at birth"
drop mg01b mg01c mg01d probir kabbir kecbir

replace mg05b = 999 if mg05b > 730 & mg04a != 1
replace mg05c = 99 if mg05c > 78 & mg04a != 1
replace mg05d = 99 if mg05d > 74 & mg04a != 1
gen kec12 = string(mg05b, "%03.0f")
gen kab12 = string(mg05c, "%02.0f")
gen pro12 = string(mg05d, "%02.0f")
gen loc12 = pro12 + kab12 + kec12
la var loc12 "location at 12yo"
replace loc12 = locbir if mg04a == 1
drop mg05b mg05c mg05d pro12 kab12 kec12 mg04a
rename mg04 typebir
rename mg08 type12
merge 1:1 pidlink using "`timelines4'", gen(_mergebir)
drop if _mergebir == 1
drop _mergebir _mergecur
save "`timelines4'", replace

*Merge with migration data after 12
use b3a_mg2, clear

*Preserve number of moves
keep pidlink movenum
collapse (max) movenum, by(pidlink)
merge 1:1 pidlink using "`timelines4'", gen (_mergenum)
replace movenum = 0 if movenum == .
drop _mergenum
save "`timelines4'", replace

*Record locations of moves
use b3a_mg2, clear
keep movenum pidlink mg21b mg21c mg21d mg24mth mg24yr mg25 mg27 mg28 mg34 mg35 mg36

replace mg21b = 999 if mg21b > 740
replace mg21c = 99 if mg21c > 78
replace mg21d = 99 if mg21d > 93

gen kecmov = string(mg21b, "%03.0f")
gen kabmov = string(mg21c, "%02.0f")
gen promov = string(mg21d, "%02.0f")
gen locmov = promov + kabmov + kecmov
drop kecmov kabmov promov mg21b mg21c mg21d

rename mg24mth mthmov
rename mg24yr yrmov
rename mg25 agemov
rename mg27 distmov
rename mg28 purpmov
rename mg34 othHHsmov
rename mg35 numHHsmov
rename mg36 whiHHsmov
rename movenum num
reshape wide *mov, i(pidlink) j(num)

merge 1:1 pidlink using "`timelines4'", gen(_mergemov)
drop _merge _mergemov

foreach x of varlist _all {
	rename `x' I4_`x'
}

rename I4_pidlink pidlink
drop if I4_locbir==""
save "`timelines4'", replace


***IFLS5

cd "$ifls5rawdata"

*Basic information
use b3a_cov, clear
keep hhid14 pidlink panel sex marstat age random_si dob_mth dob_yr result reason
rename dob_mth birmth
rename dob_yr biryr

tempfile timelines5
save "`timelines5'", replace

*Time of interview
use b3a_time, clear
keep pidlink ivwmth ivwyr time_occ
keep if time_occ == 1
drop time_occ
merge 1:1 pidlink using "`timelines5'", gen(_mergetim)
save "`timelines5'", replace

*Merge location information
use bk_sc1, clear
keep hhid14 sc01_14_14 sc02_14_14 sc03_14_14 sc05
rename sc05 ivwtype
gen kecivw = string(sc03_14_14, "%03.0f")
gen kabivw = string(sc02_14_14, "%02.0f")
gen proivw = string(sc01_14_14, "%02.0f")
gen locivw = proivw + kabivw + kecivw
la var locivw "Location HH lives when interviewed in IFLS4"
drop kecivw kabivw proivw sc01_14_14 sc02_14_14 sc03_14_14
merge 1:m hhid14 using "`timelines5'", gen(_mergecur)
drop if pidlink==""
save "`timelines5'", replace

*Merge with migration data, starting with birthplace and location at 12
use b3a_mg1, clear
keep pidlink mg01b mg01c mg01d mg04 mg05b mg05c mg05d mg08 mg04a

replace mg01b = 999 if mg01b > 740
replace mg01c = 99 if mg01c > 78
replace mg01d = 99 if mg01d > 82
gen kecbir = string(mg01b, "%03.0f")
gen kabbir = string(mg01c, "%02.0f")
gen probir = string(mg01d, "%02.0f")
gen locbir = probir + kabbir + kecbir
la var locbir "location at birth"
drop mg01b mg01c mg01d probir kabbir kecbir

replace mg05b = 999 if mg05b > 730 & mg04a != 1
replace mg05c = 99 if mg05c > 78 & mg04a != 1
replace mg05d = 99 if mg05d > 74 & mg04a != 1
gen kec12 = string(mg05b, "%03.0f")
gen kab12 = string(mg05c, "%02.0f")
gen pro12 = string(mg05d, "%02.0f")
gen loc12 = pro12 + kab12 + kec12
la var loc12 "location at 12yo"
replace loc12 = locbir if mg04a == 1
drop mg05b mg05c mg05d pro12 kab12 kec12 mg04a
rename mg04 typebir
rename mg08 type12
merge 1:1 pidlink using "`timelines5'", gen(_mergebir)
drop if _mergebir == 1
drop _mergebir _mergecur
save "`timelines5'", replace

*Merge with migration data after 12
use b3a_mg2, clear

*Preserve number of moves
keep pidlink movenum
collapse (max) movenum, by(pidlink)
merge 1:1 pidlink using "`timelines5'", gen (_mergenum)
replace movenum = 0 if movenum == .
drop _mergenum
save "`timelines5'", replace

*Record locations of moves
use b3a_mg2, clear
keep movenum pidlink mg21b mg21c mg21d mg24mth mg24yr mg25 mg27 mg28 mg34 mg35 mg36

replace mg21b = 999 if mg21b > 740
replace mg21c = 99 if mg21c > 78
replace mg21d = 99 if mg21d > 93

gen kecmov = string(mg21b, "%03.0f")
gen kabmov = string(mg21c, "%02.0f")
gen promov = string(mg21d, "%02.0f")
gen locmov = promov + kabmov + kecmov
drop kecmov kabmov promov mg21b mg21c mg21d

rename mg24mth mthmov
rename mg24yr yrmov
rename mg25 agemov
rename mg27 distmov
rename mg28 purpmov
rename mg34 othHHsmov
rename mg35 numHHsmov
rename mg36 whiHHsmov
rename movenum num
reshape wide *mov, i(pidlink) j(num)

merge 1:1 pidlink using "`timelines5'", gen(_mergemov)
drop _merge* _mergemov

foreach x of varlist _all {
	rename `x' I5_`x'
}

rename I5_pidlink pidlink
drop if I5_locbir==""
save "`timelines5'", replace



***Merging across waves

clear
use "`timelines'"
merge 1:1 pidlink using "`timelines2'", gen(_merge2)
merge 1:1 pidlink using "`timelines3'", gen(_merge3)
merge 1:1 pidlink using "`timelines4'", gen(_merge4)
merge 1:1 pidlink using "`timelines5'", gen(_merge5) 

tempfile compiled_timelines
save "`compiled_timelines'", replace


***Add additional timeline info from IFLS5 from ptrack
cd "$ifls5rawdata"
use ptrack, clear
keep sex pidlink hhid14 pid14 ar02b_14 age_14 hhid07 pid07 ar02b_07 age_07 hhid00 pid00 ar02b_00 age_00 hhid97 pid97 ar02_97 age_97 hhid93 pid93 ar02_93 age_93
duplicates drop pidlink, force
merge 1:1 pidlink using  "`compiled_timelines'", gen(_mergepp)
drop if _mergepp!=3
drop _mergepp
save "`compiled_timelines'", replace


***Process raw timelines further
use "`compiled_timelines'", clear

keep pidlink age_* *locmov* *yrmov* *agemov* *locbir *loc12 *locivw *movenum *ivwyr
rename age_00 age_2000
rename age_07 age_2007


*Generate location for each year
foreach x of numlist 1897/2015 {
	gen location_`x'=""
	gen wlocation_`x'=.
}

*Dummy indicating birth location information
gen wavebirdum = 0

***IFLS1 

local w = 1 						//wave local
local t = 1993 						//Interview year 1 in wave
local s = 93						//Age year (short)

*Birth location
replace wavebirdum = `w' if I`w'_locbir != ""

forv j = 0/5 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_locbir if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != ""
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != ""		
	}
}

*Location at 12: assumption here is that if location at 12 and location at birth are different, then moved between the two at 6.
forv j = 6/11 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_loc12 if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != ""
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != ""		
	}
}

**Moves since 12:
**In the following code: 
**k keeps track of the group of movers (those who move once, twice, etc.); 
**x keeps track of the first year all operations start for that group, based on max age
**y keeps track on the number of iterations based on the distance between `t' and x
**h is the move number currently being dealt with 
**j is the repetition number for replacement, based on y
**i is the current year being replaced

forv k = 0/14 { 						//This outer loop is going to chop up the dataset for each number of moves, so that the operations can be done on a smaller subset to increase efficiency
	preserve
	keep if I`w'_movenum == `k'			//we'll group individuals by number of moves and perform all operations on just that group each time to increase efficiency
	if `k' == 0 {						//for non-movers, we replace all locations since 12 with location at time of interview
		summarize age_`s'
		local x = `t' + 12 - r(max) 	//gives the first year to start with
		local y = `t' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//assigns location at time of ivw to every year since 12 for non-movers
			forv i = `x'/`t' {
				replace location_`i' = I`w'_locivw if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
				replace wlocation_`i' = `w' if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
			}			
		}
		macro drop x y j i 
	}
	else if `k' != 0 {					//Now we deal with movers
		summarize age_`s'
		local x = `t' + 12 - r(max) 	//gives the first year to start with
		local y = `t' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//Start by assigning location at age 12 to every year since 12 (up to `t')
			forv i = `x'/`t' {
				replace location_`i' = I`w'_loc12 if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x'
				replace wlocation_`i' = `w' if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x'
			}			
		}								
		forv h = 1/`k' { 				//Next we'll progressivelly replace years based on each move			
			gen moveage`h' = 0 			//generating the age at the move
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `s') if I`w'_yrmov`h' < 94
			replace moveage`h' = I`w'_agemov`h' if I`w'_yrmov`h' > `s' & I`w'_agemov`h' != .
			replace moveage`h' = 0 if moveage`h' < 12 | moveage`h' > 95 // 0 for the moveage will serve as an indicator to not use this move in the location calculation
			if `h' != `k' {				//First we deal with all moves except the last one
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locmov`h' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0
					}			
				}		
			}
			else if `h' == `k' { 		//when we're on the last move, we'll replace all the years since the last move with the location of the interview, to match observed data 				
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locivw if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0
					}			
				}												
			}
			drop moveage`h'
			macro drop h
		}
		macro drop x y j i
	}
	
	tempfile movers`k'
	save "`movers`k''", replace
	restore
}

**Merging the generated timelines with existing dataset
forv k = 0/14 {
	merge 1:1 pidlink using "`movers`k''", gen(_mergeb`k') replace update
	drop _merge*
}

macro drop w t s

***IFLS2

local w = 2 						//wave local
local t = 1997 						//Interview year 1 in wave
local u = 1998 						//Interview year 2 in wave
local s = 97						//Age year (short)

*Birth location
replace wavebirdum = `w' if I`w'_locbir != "" & wavebirdum == 0

forv j = 0/5 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_locbir if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
	forv i = 1897/`u' {
		replace location_`i' = I`w'_locbir if (`u'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
		replace wlocation_`i' = `w' if (`u'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
	}
}

*Location at 12: assumption here is that if location at 12 and location at birth are different, then moved between the two at 6.
forv j = 6/11 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_loc12 if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
	forv i = 1897/`u' {
		replace location_`i' = I`w'_loc12 if (`u'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
		replace wlocation_`i' = `w' if (`u'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
	}
}

**Moves since 12:

**In the following code: 
**k keeps track of the group of movers (those who move once, twice, etc.); 
**x keeps track of the first year all operations start for that group, based on max age
**y keeps track on the number of iterations based on the distance between `t' and x
**h is the move number currently being dealt with 
**j is the repetition number for replacement, based on y
**i is the current year being replaced


forv k = 0/14 { 						//This outer loop is going to chop up the dataset for each number of moves, so that the operations can be done on a smaller subset to increase efficiency
	preserve
	keep if I`w'_movenum == `k'			//we'll group individuals by number of moves and perform all operations on just that group each time to increase efficiency
	if `k' == 0 {						//for non-movers, we replace all locations since 12 with location at time of interview
		summarize age_`s'
		local x = `u' + 12 - r(max) 	//gives the first year to start with
		local y = `u' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//assigns location at time of ivw to every year since 12 for non-movers
			forv i = `x'/`t' {
				replace location_`i' = I`w'_locivw if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
				replace wlocation_`i' = `w' if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
			}			
		}
		macro drop x y j i 
	}
	else if `k' != 0 {					//Now we deal with movers
		summarize age_`s'
		local x = `t' + 12 - r(max) 	//gives the first year to start with
		local y = `t' - `x' 			//number of iterations I have to go through
		forv j = 0/`y' { 				//Start by assigning location at age 12 to every year since 12 (up to `t')
			forv i = `x'/`t' {
				replace location_`i' = I`w'_loc12 if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t' //this is the loop for those interviewed in `t'
	
				replace wlocation_`i' = `w' if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t'
				
				replace location_`i' = I`w'_loc12 if (`u'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`u' + 12 - age_`s') > `x' & I`w'_ivwyr == `u' //this is the loop for those interviewed in `u'

				replace wlocation_`i' = `w' if (`u'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`u' + 12 - age_`s') > `x' & I`w'_ivwyr == `u'
			}			
		}								
		forv h = 1/`k' { 				//Next we'll progressivelly replace years based on each move			
			gen moveage`h' = 0 			//generating the age at the move
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `t') if I`w'_yrmov`h' != . & I`w'_ivwyr == `t' //two different cases depending on when interviewed
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `u') if I`w'_yrmov`h' != . & I`w'_ivwyr == `u'
			replace moveage`h' = I`w'_agemov`h' if I`w'_yrmov`h' == . & I`w'_agemov`h' != .
			replace moveage`h' = 0 if moveage`h' < 12 | moveage`h' > 95 | (I`w'_yrmov`h' == . & I`w'_agemov`h' == .) // 0 for the moveage will serve as an indicator to not use this move in the location calculation
			if `h' != `k' {				//First we deal with all moves except the last one
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locmov`h' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
					forv i = `x'/`u' {
						replace location_`i' = I`w'_locmov`h' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
						replace wlocation_`i' = `w' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
					}
				}		
			}
			else if `h' == `k' { 		//when we're on the last move, we'll replace all the years since the last move with the location of the interview, to match observed data 				
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {	
						replace location_`i' = I`w'_locivw if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
					forv i = `x'/`u' {
						replace location_`i' = I`w'_locivw if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
						replace wlocation_`i' = `w' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
					}
				}												
			}
			drop moveage`h'
			macro drop h
		}
		macro drop x y j i
	}	
	tempfile movers2`k'
	save "`movers2`k''", replace
	restore
}

**Merging the generated timelines with existing dataset
forv k = 0/14 {
	merge 1:1 pidlink using "`movers2`k''", gen(_mergeb`k') replace update
	drop _merge*
}

macro drop w t u s


***IFLS3

local w = 3 						//wave local
local t = 2000 						//Interview year 1 in wave
local s = 2000						//Age year (short)

*Birth location
replace wavebirdum = `w' if I`w'_locbir != "" & wavebirdum == 0

forv j = 0/5 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_locbir if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}

}

*Location at 12: assumption here is that if location at 12 and location at birth are different, then moved between the two at 6.
forv j = 6/11 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_loc12 if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
}

**Moves since 12:
**In the following code: 
**k keeps track of the group of movers (those who move once, twice, etc.); 
**x keeps track of the first year all operations start for that group, based on max age
**y keeps track on the number of iterations based on the distance between `t' and x
**h is the move number currently being dealt with 
**j is the repetition number for replacement, based on y
**i is the current year being replaced


forv k = 0/12 { 						//This outer loop is going to chop up the dataset for each number of moves, so that the operations can be done on a smaller subset to increase efficiency
	preserve
	keep if I`w'_movenum == `k'			//we'll group individuals by number of moves and perform all operations on just that group each time to increase efficiency
	if `k' == 0 {						//for non-movers, we replace all locations since 12 with location at time of interview
		summarize age_`s'
		local x = `u' + 12 - r(max) 	//gives the first year to start with
		local y = `u' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//assigns location at time of ivw to every year since 12 for non-movers
			forv i = `x'/`t' {
				replace location_`i' = I`w'_locivw if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
				replace wlocation_`i' = `w' if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
			}			
		}
		macro drop x y j i 
	}
	else if `k' != 0 {					//Now we deal with movers
		summarize age_`s'
		local x = `t' + 12 - r(max) 	//gives the first year to start with
		local y = `t' - `x' 			//number of iteration have to go through
		forv j = 0/`y' { 				//Start by assigning location at age 12 to every year since 12 (up to `t')
			forv i = `x'/`t' {
				replace location_`i' = I`w'_loc12 if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t' //this is the loop for those interviewed in `t'
	
				replace wlocation_`i' = `w' if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t'

			}			
		}								
		forv h = 1/`k' { 				//Next we'll progressivelly replace years based on each move			
			gen moveage`h' = 0 			//generating the age at the move
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `t') if I`w'_yrmov`h' != . & I`w'_ivwyr == `t' //two different cases depending on when interviewed
			replace moveage`h' = I`w'_agemov`h' if I`w'_yrmov`h' == . & I`w'_agemov`h' != .
			replace moveage`h' = 0 if moveage`h' < 12 | moveage`h' > 95 | (I`w'_yrmov`h' == . & I`w'_agemov`h' == .) // 0 for the moveage will serve as an indicator to not use this move in the location calculation
			if `h' != `k' {				//First we deal with all moves except the last one
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locmov`h' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
		
				}		
			}
			else if `h' == `k' { 		//when we're on the last move, we'll replace all the years since the last move with the location of the interview, to match observed data 				
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locivw if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
		
				}												
			}
			drop moveage`h'
			macro drop h
		}
		macro drop x y j i
	}	
	tempfile movers3`k'
	save "`movers3`k''", replace
	restore
}

**Merging the generated timelines with existing dataset
forv k = 0/12 {
	merge 1:1 pidlink using "`movers3`k''", gen(_mergeb`k') replace update
	drop _merge*
}

macro drop w t u s


***IFLS4

local w = 4 						//wave local
local t = 2007 						//Interview year 1 in wave
local u = 2008 						//Interview year 2 in wave
local s = 2007						//Age year (short)

*Birth location
replace wavebirdum = `w' if I`w'_locbir != "" & wavebirdum == 0

forv j = 0/5 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_locbir if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
	forv i = 1897/`u' {
		replace location_`i' = I`w'_locbir if (`u'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
		replace wlocation_`i' = `w' if (`u'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
	}
}

*Location at 12: assumption here is that if location at 12 and location at birth are different, then moved between the two at 6.
forv j = 6/11 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_loc12 if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
	forv i = 1897/`u' {
		replace location_`i' = I`w'_loc12 if (`u'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
		replace wlocation_`i' = `w' if (`u'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
	}
}

**Moves since 12:

**In the following code: 
**k keeps track of the group of movers (those who move once, twice, etc.); 
**x keeps track of the first year all operations start for that group, based on max age
**y keeps track on the number of iterations based on the distance between `t' and x
**h is the move number currently being dealt with 
**j is the repetition number for replacement, based on y
**i is the current year being replaced


forv k = 0/12 { 						//This outer loop is going to chop up the dataset for each number of moves, so that the operations can be done on a smaller subset to increase efficiency
	preserve
	keep if I`w'_movenum == `k'			//we'll group individuals by number of moves and perform all operations on just that group each time to increase efficiency
	if `k' == 0 {						//for non-movers, we replace all locations since 12 with location at time of interview
		summarize age_`s'
		local x = `u' + 12 - r(max) 	//gives the first year to start with
		local y = `u' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//assigns location at time of ivw to every year since 12 for non-movers
			forv i = `x'/`t' {
				replace location_`i' = I`w'_locivw if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
				replace wlocation_`i' = `w' if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
			}			
		}
		macro drop x y j i 
	}
	else if `k' != 0 {					//Now we deal with movers
		summarize age_`s'
		local x = `t' + 12 - r(max) 	//gives the first year to start with
		local y = `t' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//Start by assigning location at age 12 to every year since 12 (up to `t')
			forv i = `x'/`t' {
				replace location_`i' = I`w'_loc12 if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t' //this is the loop for those interviewed in `t'
	
				replace wlocation_`i' = `w' if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t'
				
				replace location_`i' = I`w'_loc12 if (`u'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`u' + 12 - age_`s') > `x' & I`w'_ivwyr == `u' //this is the loop for those interviewed in `u'

				replace wlocation_`i' = `w' if (`u'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`u' + 12 - age_`s') > `x' & I`w'_ivwyr == `u'
			}			
		}								
		forv h = 1/`k' { 				//Next we'll progressivelly replace years based on each move			
			gen moveage`h' = 0 			//generating the age at the move
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `t') if I`w'_yrmov`h' != . & I`w'_ivwyr == `t' //two different cases depending on when interviewed
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `u') if I`w'_yrmov`h' != . & I`w'_ivwyr == `u'
			replace moveage`h' = I`w'_agemov`h' if I`w'_yrmov`h' == . & I`w'_agemov`h' != .
			replace moveage`h' = 0 if moveage`h' < 12 | moveage`h' > 95 | (I`w'_yrmov`h' == . & I`w'_agemov`h' == .) // 0 for the moveage will serve as an indicator to not use this move in the location calculation
			if `h' != `k' {				//First we deal with all moves except the last one
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locmov`h' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
					forv i = `x'/`u' {
						replace location_`i' = I`w'_locmov`h' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
						replace wlocation_`i' = `w' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
					}
				}		
			}
			else if `h' == `k' { 		//when we're on the last move, we'll replace all the years since the last move with the location of the interview, to match observed data 				
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locivw if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
					forv i = `x'/`u' {
						replace location_`i' = I`w'_locivw if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
						replace wlocation_`i' = `w' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
					}
				}												
			}
			drop moveage`h'
			macro drop h
		}
		macro drop x y j i
	}	
	tempfile movers4`k'
	save "`movers4`k''", replace
	restore
}

**Merging the generated timelines with existing dataset
forv k = 0/12 {
	merge 1:1 pidlink using "`movers4`k''", gen(_mergeb`k') replace update
	drop _merge*
}

macro drop w t u s


***IFLS5

local w = 5 						//wave local
local t = 2014 						//Interview year 1 in wave
local u = 2015 						//Interview year 2 in wave
local s = 14						//Age year (short)

*Birth location
replace wavebirdum = `w' if I`w'_locbir != "" & wavebirdum == 0

forv j = 0/5 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_locbir if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
	forv i = 1897/`u' {
		replace location_`i' = I`w'_locbir if (`u'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
		replace wlocation_`i' = `w' if (`u'+`j')-age_`s' == `i' ///
				& I`w'_locbir != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
	}
}

*Location at 12: assumption here is that if location at 12 and location at birth are different, then moved between the two at 6.
forv j = 6/11 {
	forv i = 1897/`t' {
		replace location_`i' = I`w'_loc12 if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
		replace wlocation_`i' = `w' if (`t'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `t'
	}
	forv i = 1897/`u' {
		replace location_`i' = I`w'_loc12 if (`u'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
		replace wlocation_`i' = `w' if (`u'+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & wavebirdum == `w' & I`w'_ivwyr == `u'
	}
}

**Moves since 12:
**In the following code: 
**k keeps track of the group of movers (those who move once, twice, etc.); 
**x keeps track of the first year all operations start for that group, based on max age
**y keeps track on the number of iterations based on the distance between `t' and x
**h is the move number currently being dealt with 
**j is the repetition number for replacement, based on y
**i is the current year being replaced


forv k = 0/12 { 						//This outer loop is going to chop up the dataset for each number of moves, so that the operations can be done on a smaller subset to increase efficiency
	preserve
	keep if I`w'_movenum == `k'			//we'll group individuals by number of moves and perform all operations on just that group each time to increase efficiency
	if `k' == 0 {						//for non-movers, we replace all locations since 12 with location at time of interview
		summarize age_`s'
		local x = `u' + 12 - r(max) 	//gives the first year to start with
		local y = `u' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//assigns location at time of ivw to every year since 12 for non-movers
			forv i = `x'/`t' {
				replace location_`i' = I`w'_locivw if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
				replace wlocation_`i' = `w' if (`t'+12+`j'-age_`s') == `i' ///
				& I`w'_locivw != "" & (`t' + 12 - age_`s') > `x'
			}			
		}
		macro drop x y j i 
	}
	else if `k' != 0 {					//Now we deal with movers
		summarize age_`s'
		local x = `t' + 12 - r(max) 	//gives the first year to start with
		local y = `t' - `x' 			//number of iterations have to go through
		forv j = 0/`y' { 				//Start by assigning location at age 12 to every year since 12 (up to `t')
			forv i = `x'/`t' {
				replace location_`i' = I`w'_loc12 if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t' //this is the loop for those interviewed in `t'
	
				replace wlocation_`i' = `w' if (`t'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`t' + 12 - age_`s') > `x' & I`w'_ivwyr == `t'
				
				replace location_`i' = I`w'_loc12 if (`u'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`u' + 12 - age_`s') > `x' & I`w'_ivwyr == `u' //this is the loop for those interviewed in `u'

				replace wlocation_`i' = `w' if (`u'+12+`j')-age_`s' == `i' ///
				& I`w'_loc12 != "" & (`u' + 12 - age_`s') > `x' & I`w'_ivwyr == `u'
			}			
		}								
		forv h = 1/`k' { 				//Next we'll progressivelly replace years based on each move			
			gen moveage`h' = 0 			//generating the age at the move
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `t') if I`w'_yrmov`h' != . & I`w'_ivwyr == `t' //two different cases depending on when interviewed
			replace moveage`h' = (age_`s' + I`w'_yrmov`h' - `u') if I`w'_yrmov`h' != . & I`w'_ivwyr == `u'
			replace moveage`h' = I`w'_agemov`h' if I`w'_yrmov`h' == . & I`w'_agemov`h' != .
			replace moveage`h' = 0 if moveage`h' < 12 | moveage`h' > 95 | (I`w'_yrmov`h' == . & I`w'_agemov`h' == .) // 0 for the moveage will serve as an indicator to not use this move in the location calculation
			if `h' != `k' {				//First we deal with all moves except the last one
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {
						replace location_`i' = I`w'_locmov`h' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
					forv i = `x'/`u' {
						replace location_`i' = I`w'_locmov`h' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
						replace wlocation_`i' = `w' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locmov`h' != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
					}
				}		
			}
			else if `h' == `k' { 		//when we're on the last move, we'll replace all the years since the last move with the location of the interview, to match observed data 				
				forv j = 0/`y' { 		//Here we replace all years from the year of the move to `t' with the location of the move-note the reference to the age at the move				
					forv i = `x'/`t' {	
						replace location_`i' = I`w'_locivw if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
						replace wlocation_`i' = `w' if (`t' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`t' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `t'
					}
					forv i = `x'/`u' {
						replace location_`i' = I`w'_locivw if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
						replace wlocation_`i' = `w' if (`u' + moveage`h'+`j')-age_`s' == `i' ///
						& I`w'_locivw != "" & (`u' + moveage`h' - age_`s') > `x' & moveage`h' != 0 & I`w'_ivwyr == `u'
					}
				}												
			}
			drop moveage`h'
			macro drop h
		}
		macro drop x y j i
	}	
	tempfile movers5`k'
	save "`movers5`k''", replace
	restore
}

**Merging the generated timelines with existing dataset
forv k = 0/12 {
	merge 1:1 pidlink using "`movers5`k''", gen(_mergeb`k') replace update
	drop _merge*
}

macro drop w t u s

drop I*

tempfile compiled_timelines_clean

save "`compiled_timelines_clean'", replace

***Clean location codes 

use "`compiled_timelines_clean'", clear
forv i = 1897/2015 {
	gen loccode_`i' = substr(location_`i', 1, 4)
	destring loccode_`i', replace
} 

save "`compiled_timelines_clean'", replace

*Clean crosswalk of provinces
cd "$reploc/data"
use IndoCrosswalk.dta
keep bps_*
forv i = 1993/2009 {
	gen loccode_`i' = bps_`i'
}

gen loccode_2014 = bps_2014

tempfile crosswalk
save "`crosswalk'"

*Matching with crosswalk
use "`crosswalk'", clear
keep loccode_2014 bps_*
merge 1:m loccode_2014 using "`compiled_timelines_clean'"
drop if _merge == 1
gen lc_2014_93 = bps_1993
replace lc_2014_93 = loccode_2014 if bps_1993==.
keep pidlink lc_2014_93

tempfile timelines_final
save "`timelines_final'", replace


forv i = 2009/2013 {	//These are the years that have only data from IFLS5, so use only 2014 codes
	use  "`crosswalk'", clear
	keep loccode_2014 bps_1993
	rename loccode_2014 loccode_`i'
	merge 1:m loccode_`i' using "`compiled_timelines_clean'"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993 == .
	keep pidlink lc_`i'_93
	merge 1:1 pidlink using "`timelines_final'"
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0

forv i = 2000/2008 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	
	tempfile timetemp51`x'
	save "`timetemp51`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp51`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp51`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4

	tempfile timetemp41`x'
	save "`timetemp41`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp41`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp41`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	
	tempfile timetemp31`x'
	save "`timetemp31`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp31`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp31`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	
	tempfile timetemp21`x'
	save "`timetemp21`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp21`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp21`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	
	tempfile timetemp11`x'
	save "`timetemp11`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp11`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp11`x''", replace
	
	append using "`timetemp21`x''"
	append using "`timetemp31`x''"
	append using "`timetemp41`x''"
	append using "`timetemp51`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1990/1999 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x'+1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	
	tempfile timetemp50`x'
	save "`timetemp50`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40`x'
	save "`timetemp40`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30`x'
	save "`timetemp30`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20`x'
	save "`timetemp20`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10`x'
	save "`timetemp10`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10`x''", replace
	
	append using "`timetemp20`x''"
	append using "`timetemp30`x''"
	append using "`timetemp40`x''"
	append using "`timetemp50`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl) 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1980/1989 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp500`x'
	save "`timetemp500`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp500`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp500`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp400`x'
	save "`timetemp400`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp400`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp400`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp300`x'
	save "`timetemp300`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp300`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp300`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp200`x'
	save "`timetemp200`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp200`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp200`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp100`x'
	save "`timetemp100`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp100`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp100`x''", replace
	
	append using "`timetemp200`x''"
	append using "`timetemp300`x''"
	append using "`timetemp400`x''"
	append using "`timetemp500`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1970/1979 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' +1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp5000`x'
	save "`timetemp5000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp5000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp5000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp4000`x'
	save "`timetemp4000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp4000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp4000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp3000`x'
	save "`timetemp3000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp3000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp3000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp2000`x'
	save "`timetemp2000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp2000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp2000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp1000`x'
	save "`timetemp1000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp1000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp1000`x''", replace
	
	append using "`timetemp2000`x''"
	append using "`timetemp3000`x''"
	append using "`timetemp4000`x''"
	append using "`timetemp5000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}


local x = 0
forv i = 1961/1969 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
	append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1951/1960 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
	append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1941/1950 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
	append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1931/1940 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
	append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1921/1930 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
	append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1911/1920 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
	append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1901/1910 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}

local x = 0
forv i = 1897/1900 {					//IFLS1, 2, 3, 4, 5 data
	local x = `x' + 1
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 5
	tempfile timetemp50000`x'
	save "`timetemp50000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2014 bps_1993
	rename bps_2014 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp50000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp50000`x''", replace

	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 4
	tempfile timetemp40000`x'
	save "`timetemp40000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_2007 bps_1993
	rename bps_2007 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp40000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp40000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  == 3
	tempfile timetemp30000`x'
	save "`timetemp30000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1999 bps_1993
	rename bps_1999 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp30000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp30000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i' == 2
	tempfile timetemp20000`x'
	save "`timetemp20000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1998 bps_1993
	rename bps_1998 loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp20000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp20000`x''", replace
	
	use "`compiled_timelines_clean'"	//split out the sample by when the data from the given year is
	keep if wlocation_`i'  != 2 & wlocation_`i'  != 3 & wlocation_`i'  != 4 & wlocation_`i'  != 5
	tempfile timetemp10000`x'
	save "`timetemp10000`x''", replace
	
	use "`crosswalk'", clear
	keep bps_1993
	gen bps_dum = bps_1993
	rename bps_dum loccode_`i'
	duplicates drop loccode_`i', force
	merge 1:m loccode_`i' using "`timetemp10000`x''"
	drop if _merge == 1
	gen lc_`i'_93 = bps_1993
	replace lc_`i'_93 = loccode_`i' if bps_1993==.
	keep pidlink lc_`i'_93
	save "`timetemp10000`x''", replace
	
append using "`timetemp20000`x''"
	append using "`timetemp30000`x''"
	append using "`timetemp40000`x''"
	append using "`timetemp50000`x''"
	merge 1:1 pidlink using "`timelines_final'", gen(_mergebl`k') 
	drop _merge*
	save "`timelines_final'", replace
}
order _all, alpha


save "`timelines_final'", replace


***Get consistent birth year and birth place information
use "`timelines_final'", clear

keep pidlink lc_*

*Generate birth year

gen biryear_impl = .
forv g = 1897/2014 {
	replace biryear_impl = `g' if biryear_impl == . & lc_`g'_93 != .
}
la var biryear_impl "Birth year"

*Change locations to be in terms of province code
forv g = 1897/2014 {
	replace lc_`g'_93 = floor(lc_`g'_93/100) if lc_`g'_93 != .
}

*Generate birth province
gen birprov_impl = .
forv g = 1897/2014 {
	replace birprov_impl = lc_`g'_93 if birprov_impl == . & lc_`g'_93 != .
}
la var birprov_impl "Birth province"


*Impute location if missing based on the locations on both sides of the missing
*If location is not the same on both sides, assume change half way through
*For odd number of years missing, assume the in the following province 1 year more than the previous province

gen misyrfir = .
gen misyrlas = .
gen loc_bf_mis = .
gen loc_af_mis = .

forv g = 1898/2013 {
	local i = `g' - 1
	local j = `g' + 1
	replace misyrfir = `g' if misyrfir == . & lc_`g'_93 == 99 
	replace loc_bf_mis = lc_`i'_93 if misyrfir !=. & loc_bf_mis == .
	replace misyrlas = `g' if misyrfir != . & lc_`j'_93 != 99  & misyrlas == .
	replace loc_af_mis = lc_`j'_93 if misyrlas !=. & loc_af_mis == .
	macro drop i j 
}

gen nummisyr = misyrlas - misyrfir + 1

gen halfnumlow = floor(nummisyr/2) if nummisyr != .
gen halfnumhi = ceil(nummisyr/2) if nummisyr != .
gen misyrhflow = misyrfir + halfnumlow if misyrfir != .


gen misprov_match = .
replace misprov_match = 0 if loc_bf_mis != . & loc_bf_mis != loc_af_mis
replace misprov_match = 1 if loc_bf_mis != . & loc_bf_mis == loc_af_mis

forv g = 1898/2013 {
	replace lc_`g'_93 = loc_bf_mis if misyrfir != . & `g' >= misyrfir & `g' < misyrhflow
	replace lc_`g'_93 = loc_af_mis if misyrfir != . & `g' >= misyrhflow & `g' <= misyrlas
}

drop misyrfir misyrlas loc_bf_mis loc_af_mis halfnumlow halfnumhi misyrhflow

la var nummisyr "Number of imputed province years"
la var misprov_match "Provinces on both sides of missing period match"


*Generate timelines
forv g = 1898/2014 {
	gen lcbir_`g'_93 = .
	replace lcbir_`g'_93 = birprov_impl if lc_`g'_93 != .
}

*Generate migration variables
gen migrate07 = 0
la var migrate07 "Migrate across province lines before '07"

gen migrate07_num = 0
la var migrate07_num "# of times migrate across province lines before '07"
set more off
forv g = 1898/2006 {
	local i = `g' + 1
	replace migrate07 = 1 if migrate07 == 0 & lc_`g'_93 != . & lc_`g'_93 != lc_`i'_93
	replace migrate07_num = migrate07_num + 1 if lc_`g'_93 != . & lc_`g'_93 != lc_`i'_93
	
    gen yrmig07_`g'=.	
	replace yrmig07_`g'=`g' if lc_`g'_93 != . & lc_`g'_93 != lc_`i'_93
	
	
}

gen migrate14 = 0
la var migrate14 "Migrate across province lines before '14"

gen migrate14_num = 0
la var migrate14_num "# of times migrate across province lines before '14"

forv g = 1898/2013 {
	local i = `g' + 1
	replace migrate14 = 1 if migrate14 == 0 & lc_`g'_93 != . & lc_`g'_93 != lc_`i'_93
	replace migrate14_num = migrate14_num + 1 if lc_`g'_93 != . & lc_`g'_93 != lc_`i'_93
	
	gen yrmig14_`g'=.	
	replace yrmig14_`g'=`g' if lc_`g'_93 != . & lc_`g'_93 != lc_`i'_93
	
	
}

***Generate earliest year of migration
egen earliestyrmig14=rowmin(yrmig14_*)
egen earliestyrmig07=rowmin(yrmig07_*)




